// pages/articles/rank/rank.js

const app = getApp();
const model = require('../../../utils/model');
var util = require('../../../utils/util');

function formatData(list){
  for(var i = 0; i < list.length; i ++) {
    list[i]['createtime'] = util.formatShortTime(new Date(1000*parseInt(list[i]['createtime'])));
  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navs: [
      {
        title: '浏览量'
      },
      {
        title: '参与人数'
      }
    ],
    curIndex:0,
    list:[[],[]],
    scrollHeight:0,
    lastId:[0,0],
    canLoadMore:[true,true],
    loadtips:'数据加载中...',
    showLoading:[false,false]
  },

  getList: function (index) {
    if (!this.data.canLoadMore[index]){
      return;
    }
    this.data.canLoadMore[index] = false;
    var me = this;
    model.request({
      action:'mwpdRankingList',
      data:{
        sort:index+1,
        last_id:me.data.lastId[index]
      },
      success: function (res) {
        me.data.canLoadMore[index] = true;
  
        if (res.data.list && res.data.list.length) {
          formatData(res.data.list);
          var newList = me.data.list[index].concat(res.data.list);
          var list = me.data.list;
          list[index] = newList;
          var lastId = me.data.lastId;
          lastId[index] = res.data['last_id'];
          var showLoading = me.data.showLoading;
          showLoading[index] = false;

          me.setData({
            list:list,
            lastId:lastId,
            showLoading:showLoading
          });
        }
        else {
          var showLoading = me.data.showLoading;
          showLoading[index] = false;

          me.setData({
            showLoading:showLoading
          });
          wx.showToast({
              title: '暂无更多数据',
              icon: 'loading',
              duration: 1000
          });
        }
      }
    });
  },
  
  scroll:function () {
    var showLoading = this.data.showLoading;
    showLoading[this.data.curIndex] = true;
    this.setData({
      showLoading:showLoading
    });
  },

  lower: function () {
    this.getList(this.data.curIndex);
  },

  changeView:function(event){
    console.log(event)
    var index = event.currentTarget.id;
    if(index == "0"){
      this.setData({
        curIndex: 0
      });
    }else if(index == "1"){
      this.setData({
        curIndex: 1
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scrollHeight: app.globalData.winHeight - 93
    });
    this.getList(0);
    this.getList(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})